package com.fancy.learncenter.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonWorkItemBean;
import com.fancy.learncenter.bean.CartoonWorkUnitBean;
import com.fancy.learncenter.bean.OnlyInfoBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.LoginActivity;
import com.fancy.learncenter.ui.adapter.CurrentWorkAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 当前作业
 */
public class CurrentWorkFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.unit_name)
    TextView unitName;
    @Bind(R.id.change_unit_arrow)
    ImageView changeUnitArrow;
    @Bind(R.id.unit_layout)
    RelativeLayout unitLayout;
    List<CartoonWorkItemBean> list = new ArrayList<>();
    CurrentWorkAdapter adapter;

    public CurrentWorkFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_current_work, container, false);
        ButterKnife.bind(this, view);
        initView();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        unitLayout.setVisibility(View.GONE);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        adapter = new CurrentWorkAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);

    }

    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("state", 0); // 0 未完成 1 已完成
        map.put("classId", MyApplication.classIDStr);

        HttpMehtod.getInstance().cartoonListWork(map, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<CartoonWorkUnitBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonWorkUnitBean.class);

                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);

                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonWorkUnitBean>() {
                    @Override
                    public void success(CartoonWorkUnitBean dataBean) {

//                        list.addAll(dataBean.getResult());
                        adapter.notifyDataSetChanged(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
